home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: news.athene.co.uk!not-for-mail
- From: "C.J. Scaife" <JOLTSWIFT@Athene.co.uk>
- Subject: object persistence & form()
- Message-ID: <315700AA.623E@Athene.co.uk>
- Date: Mon, 25 Mar 1996 20:23:06 +0000
- Organization: JoltSwift Ltd.
- X-Mailer: Mozilla 2.0GoldB1 (Win95; I)
- MIME-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
-
- Many initial versions of the standard C/C++ library included a
- function "char * form(const char * ...)", which was like sprintf(),
- but using it's own statically allocated buffer. This mostly seems
- to have disappeared with later releases of the respective C++
- compilers.
-
- I suspect the reason is that form() was not a good idea:
- Extensive use in string handling soon results in unintentional and
- catastrophic re-entry. The following should make it obvious what I
- mean:
-
- cout << form("My birthday is %s", form("%d March", 11));
-
- The obvious solution is to use a formatted constructor for an
- annonymous temporary instance of a string object class. In the
- following cs_sstr<120> is such a class. See
- http://www.Athene.co.uk/Joltswift/csact003.htm for explicit details.
-
- The problem with annonymous variables in C++ is that they are
- destroyed on completion of the statement in which they are
- constructed, so this works:
-
- cout << cs_cstr<120>("One statement");
-
- and this does not:
-
- char * Temp = cs_cstr<120>("Two statements");
- cout << Temp;
-
- In my humble opinion it would be much better if the usual object
- persistence rules were applicable to all automatically allocated
- objects be they annonymous or not.
-
- Does anyone know why the standards committee decided to make an
- exception for annonymous variables, or how we might take advantage of
- it ?
-
- --
- Written by Chris Scaife.
- For more details see my home page:
- http://www.Athene.co.uk/Joltswift/P1.htm
-
-